const sqlite3 = require('sqlite3').verbose();
const path = require('path');

// 连接到SQLite数据库
const dbPath = path.join(__dirname, 'database.sqlite');
const db = new sqlite3.Database(dbPath, (err) => {
  if (err) {
    console.error('数据库连接失败:', err.message);
  } else {
    console.log('已连接到SQLite数据库');
    checkData();
  }
});

// 检查数据
function checkData() {
  // 查询所有货物
  db.all(`SELECT * FROM goods`, (err, rows) => {
    if (err) {
      console.error('查询货物失败:', err.message);
    } else {
      console.log('=== 货物数据 ===');
      console.log('总共 ' + rows.length + ' 个货物:');
      rows.forEach(row => {
        console.log(`ID: ${row.id}, 名称: ${row.name}, SKU: ${row.sku}, 数量: ${row.quantity}, 位置: ${row.location}, 状态: ${row.status}`);
      });
    }
    
    // 查询所有入库单据
    db.all(`SELECT * FROM inbound_documents`, (err, rows) => {
      if (err) {
        console.error('查询入库单据失败:', err.message);
      } else {
        console.log('\n=== 入库单据数据 ===');
        console.log('总共 ' + rows.length + ' 个入库单据:');
        rows.forEach(row => {
          console.log(`ID: ${row.id}, 货物ID: ${row.goods_id}, 供应商: ${row.supplier}, 数量: ${row.quantity}, 状态: ${row.status}`);
        });
      }
      
      // 查询所有出库单据
      db.all(`SELECT * FROM outbound_documents`, (err, rows) => {
        if (err) {
          console.error('查询出库单据失败:', err.message);
        } else {
          console.log('\n=== 出库单据数据 ===');
          console.log('总共 ' + rows.length + ' 个出库单据:');
          rows.forEach(row => {
            console.log(`ID: ${row.id}, 货物ID: ${row.goods_id}, 客户: ${row.customer}, 数量: ${row.quantity}, 状态: ${row.status}`);
          });
        }
        
        // 关闭数据库连接
        db.close((err) => {
          if (err) {
            console.error('关闭数据库连接失败:', err.message);
          } else {
            console.log('\n数据库连接已关闭');
          }
        });
      });
    });
  });
}